--#include "data\config\activityconfig\DailyActivity\TianHuangConfig.lua" once   --天荒秘境
--#include "data\functions\Common\CommonBoss.lua" once				--BOSS通用功能

function GmDoTianHuang( sysarg, flag, param )
	if flag == 1 then
		if param == 0 then
			StopTianHuang()
		else
			StartTianHuang()
		end
	end
end


--[[
注册一些事件
]]
function ResisterTianHuangProc()
	--print("ResisterTianHuangProc...")
	for floorIdx, floorCfg in ipairs( TianHuangConfig.floors ) do
		SceneReliveDispatcher.register( floorCfg.sceneId, OnTianHuangActorRelive )			--注册场景、副本中角色复活触发的事件
	end
end

function InitBossCheckFunc()
	--print("InitBossCheckFunc...")
	for i, floorCfg in ipairs( TianHuangConfig.floors ) do
		for i, bossId in ipairs( floorCfg.boss or {} ) do
			--CommonBossSetCheckReliveFunc( bossId, TianHuangCheckBossRelive )
			CommonBossSetCheckMobFunc( bossId, TianHuangCheckBossMob )
		end
	end
end

--[[
检测能够复活
]]
function TianHuangCheckBossRelive( bossId )
	--print(" TianHuangCheckBossRelive, bossId="..bossId)
	return IsTianHuangValidTime()
end

--[[
检测能否Mob，防止不在活动中还刷出了怪
OnCommonCreateBoss( npcobj, bossId )会调用
]]
function TianHuangCheckBossMob( bossId )
	--print(" TianHuangCheckBossMob, bossId="..bossId)
	return IsTianHuangValidTime()
end

function OnTianHuangActorRelive( sysarg, reliveType )
	local sceneId 	= Actor.getSceneId( sysarg )
	local fubenPtr 	= Actor.getFubenPrt(sysarg)
	local fubenId 	= Fuben.getFuBenId(fubenPtr)
	--print("OnTianHuangActorRelive, reliveType="..reliveType..", sceneId="..sceneId..", fubenId="..fubenId)
	if reliveType == 2 then 			--安全副本回到皇城
		BackToMainCity( sysarg )
	end
end


--活动开启
function StartTianHuang()
	local dVar = System.getDyanmicVar()
	dVar.tianHuangBeginTime  = System.getCurrMiniTime()			--活动开启时间

	local NpcPtr = System.getGlobalNpc()
	Actor.unregScriptCallback(NpcPtr, "StopTianHuang")
	Actor.unregScriptCallback(NpcPtr, "OnTianHuangFreshMonsterLoop")    --终止刷小怪定时器

	Actor.regScriptTimer(NpcPtr, 0, TianHuangConfig.timer.activityTime*1000, TianHuangConfig.timer.activityTime*1000, 1, 
		"StopTianHuang")
	Actor.regScriptTimer(NpcPtr, 0, 0, TianHuangConfig.timer.monsterFreshCd*1000, -1, "OnTianHuangFreshMonsterLoop")

	TickTianHuangPlayerOut()				--T出残留的玩家
	FreshTianHuangBoss()					--刷出天荒BOSS

	System.broadcastTipmsg( Lang.ScriptTips.TianHuang002, ttScreenCenter + ttChatWindow )	
end

--[[活动结束
此方法，由定时器触发
]]
function StopTianHuang()
	local dVar = System.getDyanmicVar()
	dVar.tianHuangBeginTime  = nil

	local NpcPtr = System.getGlobalNpc()
	Actor.unregScriptCallback(NpcPtr, "StopTianHuang")
	Actor.unregScriptCallback(NpcPtr, "OnTianHuangFreshMonsterLoop")    --终止刷小怪定时器

	TickTianHuangPlayerOut()					--T出残留的玩家
	ClearTianHuangAllMonsters() 				--清理怪物
end

--踢出活动玩家
function TickTianHuangPlayerOut()
	for i,floorCfg in ipairs( TianHuangConfig.floors ) do
		local playerList = LuaHelp.getSceneActorListById( floorCfg.sceneId ) or {}
		for i, player in ipairs( playerList or {} ) do
			ProcExitTianHuang( player )
		end
	end
end

function ProcExitTianHuang( sysarg )
	BackToMainCity( sysarg )
end

--[[
清除天关秘境的所有怪物和BOSS
]]
function ClearTianHuangAllMonsters()
	for i, floorCfg in ipairs( TianHuangConfig.floors ) do
		for i, monCfg in ipairs( floorCfg.monsters or {} ) do
			clearScenceMonster( monCfg ) 		--清除小怪
		end

		for i, bossId in ipairs( floorCfg.boss or {} ) do
			local bossCfg = MobBossCommonConfig[bossId]
			if bossCfg then
				clearScenceMonster( bossCfg )		--清除BOSS
				KillCommonDeadMonster( bossId )		--清除BOSS对应的死亡怪
			end
		end
	end
end

--[[
是否在活动中
]]
function IsTianHuangValidTime( )
	local now 	= System.getCurrMiniTime()
	local dVar 	= System.getDyanmicVar()
	if dVar.tianHuangBeginTime and dVar.tianHuangBeginTime > 0 and now-dVar.tianHuangBeginTime >=0 and
		now-dVar.tianHuangBeginTime < TianHuangConfig.timer.activityTime then
		return true
	end
	return false
end


--[[
返回值：0-天荒秘境之外，1-天荒秘境内（非顶层），2-天荒秘境内（顶层）
]]
function TianHuangGetFloorSts( sceneId )
	local topSceneId = TianHuangConfig.floors[#TianHuangConfig.floors].sceneId 				--顶层场景
	for floorIdx, floorCfg in ipairs( TianHuangConfig.floors ) do
		if floorCfg.sceneId == sceneId then
			if sceneId == topSceneId then
				return 2
			else
				return 1
			end
		end
	end
	return 0
end

--[[
是否在场景内
]]
function IsInTianHuang( sceneId )
	--print("IsInTianHuang, sceneId="..sceneId)
	for i,floorCfg in ipairs( TianHuangConfig.floors ) do
		if sceneId == floorCfg.sceneId then
			return true
		end
	end
	return false
end


function GetTianHuangFloorCfgBySceneId( sceneId )
	for i,floorCfg in ipairs( TianHuangConfig.floors ) do
		if floorCfg.sceneId == sceneId then
			return floorCfg
		end
	end
	return nil
end

--[[
进入第N层
]]
function EnterTianHuang( sysarg )
	if not IsTianHuangValidTime() then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0038, ttFlyTip)
		return
	end

	local sceneId 	= Actor.getSceneId( sysarg )
	local floorSts 	= TianHuangGetFloorSts( sceneId )
	if floorSts ~= 0 then 		--在场景外
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0032, ttFlyTip)
		return false
	end

	local floorCfg = TianHuangConfig.floors[1]
	if not floorCfg then
		return
	end

	if CommonEnterScence( sysarg, floorCfg, GameLog.clTianHuangEnterFee, Lang.ScriptTips.TianHuangLog01 ) then

	end
end

--[[
进入下一层
]]
function NextTianHuang( sysarg )
	--print("NextTianHuang...")
	if not IsTianHuangValidTime() then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0038, ttFlyTip)
		return
	end

	local floorCfg = GetTianHuangFloorCfgBySceneId( Actor.getSceneId( sysarg ) )
	if not floorCfg then 			--不在场景中，请先进入
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.TianHuang004, ttFlyTip)
		return
	end

	local nextFloorIdx = floorCfg.floorIdx + 1
	local nextFloorCfg = TianHuangConfig.floors[nextFloorIdx]
	if not nextFloorCfg then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.TianHuang005, ttFlyTip)
		return
	end

	if CommonEnterScence( sysarg, nextFloorCfg, GameLog.clTianHuangEnterFee, Lang.ScriptTips.TianHuangLog01 ) then
		
	end
end

--[[
退出天荒秘境
]]
function TianHuangQuit( sysarg )
	local sceneId = Actor.getSceneId( sysarg )
	if IsInTianHuang( sceneId ) then
		BackToMainCity( sysarg )
	end
end

--[[
刷出天荒秘境BOSS
]]
function FreshTianHuangBoss()
	local openDays = System.getDaysSinceOpenServer() 
	for floorIdx, floorCfg in ipairs( TianHuangConfig.floors ) do
		if openDays >= floorCfg.openServerDay then 			--开服时间不到，不刷BOSS
			FreshCommonBossById( floorCfg.boss )
		end
	end
end

function OnTianHuangFreshMonsterLoop(  )
	--print("OnTianHuangFreshMonsterLoop...")
	FreshTianHuangMonsters( )
end

--[[
刷出天荒秘境的小怪
]]
function FreshTianHuangFloorMonsters( floorIdx )
	--print("FreshTianHuangMonsters, floorIdx="..floorIdx)
	local floorCfg = TianHuangConfig.floors[floorIdx]		--从第1层开始
	if not floorCfg then
		return
	end

	for i, monCfg in ipairs( floorCfg.monsters or {} ) do
		freshSceneMonsterInRange( monCfg )
	end
end

--[[
刷出天荒秘境怪物
]]
function FreshTianHuangMonsters( )
	--print("FreshTianHuangMonsters...")
	for floorIdx, floorCfg in ipairs( TianHuangConfig.floors ) do
		FreshTianHuangFloorMonsters( floorIdx )
	end
end

ResisterTianHuangProc()
InitBossCheckFunc()
